package util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class BaseUtil {
	
	public static String Md5(String source, String mdType){
		
		MessageDigest digest = null ;
		
		try {
			digest = MessageDigest.getInstance(mdType);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		
		byte [] md = digest.digest(source.getBytes()) ;
		
		StringBuilder sb = new StringBuilder() ;
		
		for(byte b : md){
			String stemp = Integer.toHexString(b) ;
			int len = stemp.length() ;
			if(len>2){
				stemp = stemp.substring(len-2) ;
			}else if(len<2){
				stemp += 2 ;
			}
			sb.append(stemp) ;
		}
		
		return sb.toString() ;
		
	}
	
	/*public static void main(String[] args) {
		String s = BaseUtil.Md5("sdjflojrfolgv","md5") ;
		System.out.println(s);
		System.out.println(s.length());
	}*/
}
